<?php
/**
 * 
 */

/**
 * 
 */

/**
 * Description of user
 *
 * @author anthony
 */
class User implements Stashable {
    // Class Variables
    protected $isValid = false;
    
    // User Information
    protected $userid = false;
    protected $username = false;
    protected $email = false;
    protected $priv_adm = 0;
    protected $profile_icon = false;
    
    protected $projects = false;
    /**
     * Constructor for the user class, instanciates if possible
     * @param string/int $user_identifier
     * @return boolean 
     */
    public function __construct($user_identifier) {
	$this->user_identifier = $user_identifier;
	try {
	    $row = &$this->resolveIdentifier();
	    // Existance Checking
	    if($row === false) {
		$this->isValid = false;
		return false;
	    } else {
		
		// -------- Setup the initial values that are to be used! --------- //
		// ---- Remember that there is not a need to instanciate unless you need all values ---- //
		$this->isValid = true;
		// Row is an assoc array =)
		$this->userid = $row['uid'];
		$this->username = $row['username'];
		$this->email = $row['email'];
		$this->priv_adm = (((int)$row['priv_adm']) === 1 ? true : false);
		$this->priv_ban = (((int)$row['priv_ban']) === 1 ? true : false);
		$this->profile_icon = strlen($row['profile_icon']) !== 0 ? $row['profile_icon'] : '/img/nopic.jpg';
		return true;
	    }
	} catch(InvalidArgumentException $e) {
	    // Someone passed null
	    $this->user_id = -1;
	    $this->isValid = false;
	    return false;
	}
    }
    
    /**
     * There are two ways of resolving the user, we can use their username or their user id.
     * This function will resolve that and return the database row for that user
     * @global type $sql_conx
     * @return boolean/array
     * @throws InvalidArgumentException 
     */
    protected function &resolveIdentifier() {
	global $sql_conx;
	$res_by_num = null;
	$res_by_name = null;
	
	// Do we trust this data?
	$sqlSafe_uident = $sql_conx->real_escape_string($this->user_identifier);
	
	// Fields to grab
	$fields_to_get = '*';//`username`, `email`, `priv_adm`';
	
	// Try to resolve the pid if it is an integer
	if(is_null($this->user_identifier)) {	    
	    throw new InvalidArgumentException("Cannot resolve null.");
	} elseif(is_int($this->user_identifier)) {
	    $query = $sql_conx->query("SELECT {$fields_to_get} FROM `users` WHERE `uid`='{$sqlSafe_uident}' LIMIT 1;");
	    if($query !== false && mysqli_num_rows($query) === 1) {
		$row = $query->fetch_assoc();
		return $row;
	    }
	    trigger_error('Could not resolve via id number `'.$this->user_identifier.'`');
	    unset($query);
	    $res_by_num = false;
	}
	// Try to resolve the alias
	$query = $sql_conx->query("SELECT {$fields_to_get} FROM `users` WHERE `username`='{$sqlSafe_uident}' LIMIT 1;");
	if($query !== false && mysqli_num_rows($query) === 1) {
	    $row = $query->fetch_assoc();
	    return $row;
	} else {
	    $res_by_name = false;
	    trigger_error('Could not resolve via username `'.$this->user_identifier.'`');
	}
	unset($query);
	$false = false; // returning by reference, slave variable
	return $false;
    }
    
    /**
     * Destructor for the class 
     */
    public function __destruct() {}
    
    /**
     * Is the user valid?
     * @return boolean
     */
    public function isValid() {
	return $this->isValid;
    }
    
    /**
     * Return the user id
     * @return int 
     */
    public function get_userid() {
	return $this->userid;
    }
    
    /**
     * Return the users username
     * @return string 
     */
    public function get_username() {
	return $this->username;
    }
    
    /**
     * Return the users email
     * @return string 
     */
    public function get_email() {
	return $this->email;
    }
    
    /**
     * Is the user an admin?
     * @return boolean
     */
    public function get_priv_adm() {
	return $this->priv_adm;
    }
    
    /**
     * Is the user banned
     * @return boolean
     */
    public function get_priv_ban() {
	return $this->priv_ban;
    }
    
    /**
     * Return the profile icon for the user
     * @return string 
     */
    public function get_profile_icon() {
	return $this->profile_icon;
    }
    
    /**
     * Return the profile link for the user
     * @return string 
     */
    public function get_profile_url() {
	return EclipseMDE::userLink($this->get_username());
    }
    
    /**
     * Return an array of all project IDs the user is involved with 
     */
    public function get_projects() {
	global $sql_conx;
	if($this->projects === false) {
	    $returnarray = array();
	    $query = $sql_conx->query("SELECT `pid` FROM `tool_project`");
	    if($query !== false) {
		while($row = $query->fetch_row()) {
		    array_push($returnarray, $row[0]);
		}
	    }
	    $this->projects = $returnarray;
	    return $returnarray;
	}
	return $this->projects; 
    }
}

?>
